package backstage.app.controller.pt;

import java.time.LocalDateTime;

import java.util.List;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

 

import backstage.app.entity.ConstionParent;
import backstage.app.entity.Constitution;
import backstage.app.entity.InforExport;
import backstage.app.entity.Student;
import backstage.app.entity.StudentType;
import backstage.app.mapper.ConstionParentMapper;
import backstage.app.mapper.ConstitutionMapper;
import backstage.app.mapper.InforExportMapper;
import backstage.app.mapper.StudentMapper;
import backstage.app.mapper.StudentTypeMapper;
import backstage.app.respont.RespontDateJson;
import backstage.app.service.ConstitutionService;
import backstage.app.service.impl.util.ConstitutionImplUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("${spring.application.name}/constitution/api")
@Api("体质测试-控制器")
public class ConstitutionController {
	@Autowired
	private ConstitutionService constitutionService;
	@Autowired
	private ConstitutionMapper constitutionMapper;
	@Autowired
	private ConstionParentMapper constionParentMapper;
	private static final Logger logger = (Logger) LoggerFactory.getLogger(ConstitutionController.class);
	 @Autowired
	private InforExportMapper inforExportMapper;
	@Autowired
	private ConstitutionImplUtil constitutionImplUtil;
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private StudentTypeMapper studentTypeMapper;
	
	
	@PostMapping("/listBySid")
	@ApiOperation(value="根据sid查询")
	@ApiImplicitParams({
		@ApiImplicitParam(value="sid")
	})
	public RespontDateJson<?>listBySid(@RequestParam(value="sid",required = true)Integer sid){
		List<Constitution>list=null;
		try {
			 list=constitutionMapper.selectBySid(sid);
			 for (Constitution constitution : list) {
				 ConstionParent constionParent = constionParentMapper.selectByPrimaryKey(constitution.getCpId());
				 
				 constitution.setConstionParent(constionParent);
			}	 
		} catch (Exception e) {
			logger.error("查询体质测试时报错: {}",sid);
		}
		return RespontDateJson.ok(list, "成功");
	}
	
	
	
	
	@PostMapping("/deleteByCid")
	@ApiOperation(value="根据cid删除")
	@ApiImplicitParams({
		@ApiImplicitParam(value="cid")
	})
	public RespontDateJson<?>deleteByCid(@RequestParam(value="cid",required = true)Integer cid){
		 int result = -1;
		try {
			result=constitutionService.deleteByPrimaryKey(cid);
			return RespontDateJson.ok(result, "成功");
		} catch (Exception e) {
			logger.error("删除体质测试时报错: {}",result);
			return RespontDateJson.ok(result, "删除体质测试时报错");
		}
		 
		 
	}
	
	@PostMapping("/selectByCid")
	@ApiOperation(value="根据cid查询")
	@ApiImplicitParams({
		@ApiImplicitParam(value="cid")
	})
	public RespontDateJson<?>selectByCid(@RequestParam(value="cid",required = true)Integer cid){
		Constitution result = null;
		try {	 
			result=constitutionService.selectByPrimaryKey(cid);
			ConstionParent constionParent = constionParentMapper.selectByPrimaryKey(result.getCpId());
			result=constitutionImplUtil.getConstitution1(result);
			result.setConstionParent(constionParent);				 
			return RespontDateJson.ok(result, "成功");			 
		} catch (Exception e) {
			logger.error("查询体质测试时报错: {}",result);
			return RespontDateJson.ok(result, "查询体质测试时报错");
		}
		 
		 
	}
	 
	
	
	@PostMapping("/selectConstionParentByCid")
	@ApiOperation(value="根据cid查询")
	@ApiImplicitParams({
		@ApiImplicitParam(value="cid")
	})
	public RespontDateJson<?>selectConstionParentByCid(@RequestParam(value="cid",required = true)Integer cid){
		Constitution result = null;
		try {	 
			result=constitutionService.selectByPrimaryKey(cid);
			ConstionParent constionParent = constionParentMapper.selectByPrimaryKey(result.getCpId());
			result=constitutionImplUtil.getConstitution1(result);
			/**体质测试主表**/
			result.setConstionParent(constionParent);	
			/**体质测试表*/
			InforExport inforExport=inforExportMapper.selectByPrimaryKeyConstid(constionParent.getCid());
			System.out.println(inforExport);
			result.setInforExport(inforExport);
			/**注入学生*/
			Student student = studentMapper.selectByPrimaryKey(result.getSid());
			/**注入类型标签**/
			StudentType studenttype=studentTypeMapper.selectByStudentType(student.getStudentType());
			student.setTypeStudent(studenttype);
			result.setStudent(student);
			
			return RespontDateJson.ok(result, "成功");			 
		} catch (Exception e) {
			logger.error("查询体质测试时报错: {}",result);
			return RespontDateJson.ok(result, "查询体质测试时报错");
		}
		 
		 
	}
 
	@PostMapping("/insert")
	@ApiOperation(value="添加")
	@ApiImplicitParams({
		@ApiImplicitParam(value="conditionType类型技能")
	})
	public RespontDateJson<?>insert(
			@RequestParam(value="sid",required = true)Integer sid,
			//学生对应id
			@RequestParam(value="conditionType",required = false)Integer conditionType,
			@RequestParam(value="height",required = false)double height,
			@RequestParam(value="width",required = false)double width,
			@RequestParam(value="abduction",required = false)double abduction,
			@RequestParam(value="fingerSpan",required = false)double fingerSpan,
			//大文本
			@RequestParam(value="singForward",required = false)double singForward,
			@RequestParam(value="roundTrip",required = false)Integer roundTrip,
			@RequestParam(value="courtSprint",required = false)double courtSprint,
			@RequestParam(value="restrtedArea",required = false)double restrtedArea,
			@RequestParam(value="klingWing",required = false)double klingWing,
			@RequestParam(value="verticalJump",required = false)Integer verticalJump,
			@RequestParam(value="continJump",required = false)Integer continJump,
			//备注
			@RequestParam(value="remake",required = false)String remake){
		 return constitutionImplUtil.insert(sid, conditionType, height, width, abduction, fingerSpan, singForward,
				 roundTrip, courtSprint, restrtedArea, klingWing, verticalJump, continJump, remake);
	}
	
	
	@PostMapping("/update")
	@ApiOperation(value="修改")
	@ApiImplicitParams({
		@ApiImplicitParam(value="conditionType类型技能")
	})
	public RespontDateJson<?>update(
			@RequestParam(value="cid",required = true)Integer cid,
			//学生对应id
			 
			@RequestParam(value="height",required = false)Double height,
			@RequestParam(value="width",required = false)Double width,
			@RequestParam(value="abduction",required = false)Double abduction,
			@RequestParam(value="fingerSpan",required = false)Double fingerSpan,
			//大文本
			@RequestParam(value="singForward",required = false)Double singForward,
			@RequestParam(value="roundTrip",required = false)Integer roundTrip,
			@RequestParam(value="courtSprint",required = false)Double courtSprint,
			@RequestParam(value="restrtedArea",required = false)Double restrtedArea,
			@RequestParam(value="klingWing",required = false)Double klingWing,
			@RequestParam(value="verticalJump",required = false)Integer verticalJump,
			@RequestParam(value="continJump",required = false)Integer continJump,
			//备注
			@RequestParam(value="remake",required = false)String remake){
		logger.info("进入后台");
		Constitution constitution=new Constitution();
		constitution.setCid(cid);
		constitution.setHeight(height);
		//对应
		constitution.setWeight(width);
		constitution.setAbduction(abduction);
		constitution.setFingerSpan(fingerSpan);
		constitution.setSingForward(singForward);
		constitution.setRoundTrip(roundTrip);
		constitution.setCourtSprint(courtSprint);
		constitution.setRestrtedArea(restrtedArea);
		constitution.setKlingWing(klingWing);
		constitution.setVerticalJump(verticalJump);
		constitution.setContiNump(continJump);
		constitution.setRemake(remake);
		constitution.setUpdateTime(LocalDateTime.now());
	
		int result=-1;
		try {
			  result=constitutionMapper.updateByPrimaryKey(constitution);
			  return result>0?RespontDateJson.ok(result, "修改成功"):RespontDateJson.fuall(result,"修改失败，出现未知异常，请联系开发人员");
		} catch (Exception e) {
			logger.error("学员修改体质测试时，数据库发生异常！");
			return RespontDateJson.fuall(result, "学员修改体质测试时，数据库发生异常！");
		}
		 
		 
	}
	
}
